<template>
  <base-load-page :loadState="false" class="base-load-page">
    <template #head>
      <div style="padding-bottom: 15px" class="head-box">
        <div class="left-box">
          <base-input-search
            v-model="filterData.wxFinderId"
            clearable
            placeholder="微信视频号作者Id"
            style="width: 230px"
            :prefixIcon="IconSearch"
            @submit="getAxiosData(true)"
          ></base-input-search>
          <base-input-search
            v-model="filterData.wxFinderName"
            clearable
            placeholder="视频号名称"
            style="width: 230px"
            :prefixIcon="IconSearch"
            @submit="getAxiosData(true)"
          ></base-input-search>
          <base-input-search
            v-model="filterData.createName"
            clearable
            placeholder="投手名称"
            style="width: 230px"
            :prefixIcon="IconSearch"
            @submit="getAxiosData(true)"
          ></base-input-search>
        </div>
        <div class="right-box">
          <el-button type="success" :icon="IconPlus" @click="handleAddPromotionLink()">新增</el-button>
        </div>
      </div>
    </template>
    <el-table ref="elTableRef" border height="100%" :data="tableData" showOverflowTooltip stripe style="width: 100%">
      <template #empty>
        <base-empty style="width: 100%" :description="description" :pageLoadButton="[{ text: '重新加载', callBack: () => getAxiosData() }]"></base-empty>
      </template>
      <el-table-column
        fixed
        type="index"
        :index="(index: number) => (filterData.current - 1) * filterData.size + index + 1"
        label="编号"
        sortable
        width="60"
        align="center"
        headerAlign="center"
      ></el-table-column>
      <el-table-column sortable label="微信视频号作者Id" prop="wxFinderId" minWidth="120"></el-table-column>
      <el-table-column sortable label="视频号名称" prop="wxFinderName" minWidth="120"></el-table-column>
      <el-table-column sortable label="部门" prop="useOrigin" minWidth="120">
        <template #default="{ row }">
          {{ row.useOrigin === 1 ? '投放部门' : row.useOrigin === 2 ? '星图部门' : '' }}
        </template>
      </el-table-column>
      <el-table-column sortable label="投手名称" prop="createName" minWidth="120"></el-table-column>
      <el-table-column sortable label="短剧" prop="dramaTitle" minWidth="120"></el-table-column>
      <el-table-column sortable label="单集解锁价格（K币）" prop="price" minWidth="120"></el-table-column>
      <el-table-column sortable label="起始付费集数" prop="rechargeEpisodeNum" minWidth="120"></el-table-column>
      <el-table-column sortable label="创建时间" prop="createTime" minWidth="120"></el-table-column>
      <el-table-column fixed="right" label="操作" width="100" align="center" headerAlign="center">
        <template #default="{ row }">
          <div style="display: flex; flex-direction: column; justify-content: space-around; height: 80px; overflow: hidden">
            <el-button link type="primary" size="small" style="margin-left: 0" :icon="IconEdit" @click="handleEditPromotionLink(row)">编辑</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <template #foot>
      <div class="pagination-box">
        <el-pagination
          v-model:currentPage="filterData.current"
          v-model:pageSize="filterData.size"
          :pageSizes="[20, 30, 40, 50]"
          :disabled="baseData.loading"
          :pagerCount="13"
          layout="total, prev, pager, next, sizes, jumper"
          :total="baseData.total"
          @change="getAxiosData(true)"
        ></el-pagination>
      </div>
    </template>
    <add-promotion-link ref="addPromotionLinkRef"></add-promotion-link>
    <edit-template ref="editTemplateRef"></edit-template>
  </base-load-page>
</template>

<script setup lang="ts">
  import IconSearch from '~icons/ep/search';
  import IconPlus from '~icons/ep/plus';
  import { type GetReferralRecordListRequest, type ReferralRecord, tencentFinderRecordPage } from '@/api/marketing-management';
  import AddPromotionLink from './components/add-promotion-link.vue';
  import IconEdit from '~icons/ep/edit';
  import EditTemplate from './components/edit-template.vue';

  const addPromotionLinkRef = ref<InstanceType<typeof AddPromotionLink>>();
  const editTemplateRef = ref<InstanceType<typeof EditTemplate>>();
  const elTableRef: $RefComp<'ElTable'> = ref(null);
  const baseData = shallowReactive({
    /** 加载状态 */
    loading: true,
    /** 总数量 */
    total: 0,
  });

  const filterData: Ref<GetReferralRecordListRequest> = ref<GetReferralRecordListRequest>({
    current: 1,
    size: 40,
    wxFinderId: '',
    wxFinderName: '',
  });

  const tableData: Ref<ReferralRecord[]> = ref([]);
  const description = ref('');
  onMounted(() => {
    getAxiosData();
  });

  onBeforeRouteLeave(() => {
    addPromotionLinkRef.value?.hide();
    editTemplateRef.value?.hide();
  });

  /**
   * 设置表格数据
   */
  async function getAxiosData(showLoading = true) {
    try {
      await nextTick();
      baseData.loading = Boolean(showLoading);
      const res = await tencentFinderRecordPage(filterData.value);
      tableData.value = res.data.records;
      description.value = '暂无数据';
      filterData.value.current = res.data.current;
      filterData.value.size = res.data.size;
      baseData.total = res.data.total;
      baseData.loading = false;
    } catch (error) {
      baseData.loading = false;
      tableData.value = [];
      description.value = error.msg || '加载失败，-BD001';
    }
  }

  /** 新增推广链接 */
  function handleAddPromotionLink() {
    addPromotionLinkRef.value?.show({
      action: 'add',
      addCallBack() {
        getAxiosData();
      },
    });
  }

  /** 编辑推广链接 */
  function handleEditPromotionLink(row: ReferralRecord) {
    addPromotionLinkRef.value?.show({
      action: 'edit',
      referralRecord: row,
      editCallBack() {
        getAxiosData();
      },
    });
  }

  /** 编辑价格模版 */
  // function handleEditTemplate(row: ReferralRecord) {
  //   editTemplateRef.value?.show({
  //     referralLinkId: row.referralLinkId!,
  //     callBack() {
  //       getAxiosData();
  //     },
  //   });
  // }
</script>

<style lang="scss" scoped>
  .pagination-box {
    @include flex(row, nowrap, center, center, center);
    padding-top: 15px;
  }

  .head-box {
    @include flex(row, nowrap, flex-start, center, center);

    .left-box {
      // display: grid;
      display: flex;
      flex: 1;
      flex-wrap: wrap;
      // grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
      // grid-gap: 15px;
      gap: 14px;
      margin-right: 15px;
    }
  }

  .content-wrap {
    padding: 8px 0;
    white-space: wrap;
  }
</style>
